using AutoMapper;
using UwinEducation.Application.Common.Interfaces;
using UwinEducation.Application.Dtos;
using UwinEducation.Domain.Entities;
using UwinEducation.Domain.IRepositories;
using UwinEducation.Shared.Models;

namespace UwinEducation.Application.Services
{
    /// <summary>
    /// 教室服务实现
    /// </summary>
    public class ClassroomService : IClassroomService
    {
        private readonly IClassroomRepository _classroomRepository;
        private readonly IMapper _mapper;

        public ClassroomService(IClassroomRepository classroomRepository, IMapper mapper)
        {
            _classroomRepository = classroomRepository;
            _mapper = mapper;
        }

        /// <summary>
        /// 获取教室分页列表
        /// </summary>
        public async Task<PagedResult<ClassroomDto>> GetPagedListAsync(ClassroomPageRequest pageRequest)
        {
            var result = await _classroomRepository.GetPagedListAsync(pageRequest);
            return new PagedResult<ClassroomDto>
            {
                Total = result.Total,
                Items = _mapper.Map<List<ClassroomDto>>(result.Items)
            };
        }

        /// <summary>
        /// 获取教室详情
        /// </summary>
        public async Task<ClassroomDto> GetByIdAsync(Guid id)
        {
            var classroom = await _classroomRepository.GetDetailsByIdAsync(id);
            if (classroom == null)
            {
                throw new ArgumentException($"教室不存在: {id}");
            }

            return _mapper.Map<ClassroomDto>(classroom);
        }

        /// <summary>
        /// 创建教室
        /// </summary>
        public async Task<ClassroomDto> CreateAsync(CreateClassroomDto input)
        {
            // 检查名称是否已存在
            if (await _classroomRepository.ExistsByNameAsync(input.Name))
            {
                throw new ArgumentException($"教室名称已存在: {input.Name}");
            }

            var classroom = Classroom.Create(input.Name,input.Capacity, input.Remarks ?? string.Empty);
            await _classroomRepository.AddAsync(classroom);

            return await GetByIdAsync(classroom.Id);
        }

        /// <summary>
        /// 更新教室
        /// </summary>
        public async Task<ClassroomDto> UpdateAsync(UpdateClassroomDto input)
        {
            var classroom = await _classroomRepository.GetByIdAsync(input.Id);
            if (classroom == null)
            {
                throw new ArgumentException($"教室不存在: {input.Id}");
            }

            // 检查名称是否已存在
            if (await _classroomRepository.ExistsByNameAsync(input.Name, input.Id))
            {
                throw new ArgumentException($"教室名称已存在: {input.Name}");
            }

            classroom.Name = input.Name;
            classroom.Remarks = input.Remarks?? classroom.Remarks;

            await _classroomRepository.UpdateAsync(classroom);

            return await GetByIdAsync(classroom.Id);
        }

        /// <summary>
        /// 删除教室
        /// </summary>
        public async Task DeleteAsync(Guid id)
        {
            var classroom = await _classroomRepository.GetByIdAsync(id);
            if (classroom == null)
            {
                throw new ArgumentException($"教室不存在: {id}");
            }

            await _classroomRepository.DeleteAsync(id);
        }
    }
} 